package com.talkyun.utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

@SuppressWarnings({"all"})
public class MapUtils {
    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN);

    public static String getString(Map map, String... keys) {
        for (String key : keys) {
            String val = getFromMap(map, key);
            if (isNotBlank(val)) {
                return val.trim();
            }
        }
        return null;
    }

    public static String getStringFromMaps(Map[] maps, String... keys) {
        for (Map map : maps) {
            String val = getString(map, keys);
            if (isNotBlank(val)) {
                return val.trim();
            }
        }
        return null;
    }

    private static String getFromMap(Map map, String key) {
        key = key.trim();
        for (Object o : map.entrySet()) {
            Map.Entry entry = (Map.Entry) o;
            if (entry.getKey() == null || entry.getValue() == null) {
                continue;
            }
            String tmp = entry.getKey().toString().trim();
            if (key.equalsIgnoreCase(tmp)) {
                return entry.getValue().toString().trim();
            }
        }
        return null;
    }

    public static String extractStringValue(Map<String, Object> output, String path) {
        // 首先定位到最后一个Map节点
        String[] split = path.split(".");
        Map<String, Object> map = output;
        for (int i = 0; i < split.length - 1; i++) {
            map = (Map<String, Object>) map.get(split[i]);
        }

        // 获取数据
        return String.valueOf(map.get(split[split.length - 1]));
    }

    public static Integer extractIntegerValue(Map<String, Object> output, String path) {
        // 首先定位到最后一个Map节点
        String[] split = path.split(".");
        Map<String, Object> map = output;
        for (int i = 0; i < split.length - 1; i++) {
            map = (Map<String, Object>) map.get(split[i]);
        }

        // 获取数据
        Object data = map.get(split[split.length - 1]);
        return data == null ? null : Integer.valueOf(String.valueOf(data));
    }

    public static Long extractLongValue(Map<String, Object> output, String path) {
        // 首先定位到最后一个Map节点
        String[] split = path.split(".");
        Map<String, Object> map = output;
        for (int i = 0; i < split.length - 1; i++) {
            map = (Map<String, Object>) map.get(split[i]);
        }

        // 获取数据
        Object data = map.get(split[split.length - 1]);
        return data == null ? null : Long.valueOf(String.valueOf(data));
    }

    public static LocalDateTime extractLocalDateTimeValue(Map<String, Object> output, String path) {
        // 首先定位到最后一个Map节点
        String[] split = path.split(".");
        Map<String, Object> map = output;
        for (int i = 0; i < split.length - 1; i++) {
            map = (Map<String, Object>) map.get(split[i]);
        }

        // 获取数据
        Object data = map.get(split[split.length - 1]);
        return data == null ? null : LocalDateTime.parse(String.valueOf(data), DATE_TIME_FORMATTER);
    }

    private static boolean isNotBlank(String s) {
        return s != null && s.trim().length() != 0;
    }
}
